const attributes = ["value", "placeholder", "title", "label", "aria-label", "aria-labelledby", "text"];

export const findElementByValue = (value: string, filter: Function) : (Node | null) => {
  const elements = document.querySelectorAll(`*[value='${value}']`);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}

export const findElementByPlaceholder = (placeholder: string, 
  documentCtx: (Document | null), filter: Function) : (Node | null) => {
  if(!documentCtx) {
    return null;
  }
  const elements = document.querySelectorAll(`*[placeholder='${placeholder}']`);
  for(const ele of elements) {
    return ele;
  }
  return null;
}

export const findElementByTitle = (title: string, filter: Function) : (Node | null) => {
  const elements = document.querySelectorAll(`*[title='${title}']`);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}

export const findElementByLabel = (label: string, filter: Function) : (Node | null) => {
  const elements = document.querySelectorAll(`*[label='${label}']`);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}

export const findElementByAriaLabel = (ariaLabel: string, filter: Function) : (Node | null) => {
  const elements = document.querySelectorAll(`*[aria-label='${ariaLabel}']`);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}

export const findElementByAriaLabelledby = (ariaLabelledby: string, filter: Function) : (Node | null) => {
  const elements = document.querySelectorAll(`*[aria-labelledby='${ariaLabelledby}']`);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}

export const findElementByText = (text: string, filter: Function) : (Node | null) => {
  const elements = findElementByXpath(`*[text()='${text}']`, document);
  for(const ele of elements) {
    filter(ele);
  }
  return null;
}


export const  findElementByXpath =  (data, documentCtx = document) => {
    const elements = documentCtx.evaluate(
      data.selector,
      documentCtx,
      null,
      XPathResult.ORDERED_NODE_ITERATOR_TYPE,
      null
    );
    return elements.iterateNext();
    // let element = elements.iterateNext();
}
